package com.ruoyi.work.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.ResponseModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.work.domain.BillReportVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.work.domain.WorkBill;
import com.ruoyi.work.service.IWorkBillService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 计件工作清单Controller
 *
 * @author xwj
 * @date 2024-08-14
 */
@RestController
@RequestMapping("/system/workBill")
@Api(tags = "计件工作清单")
public class WorkBillController extends BaseController {
    @Autowired
    private IWorkBillService workBillService;

    /**
     * 查询计件工作清单列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public TableDataInfo list(@RequestBody WorkBill workBill) {
        startPage();
        List<WorkBill> list = workBillService.selectWorkBillList(workBill);
        return getDataTable(list);
    }

    @PostMapping("/listAll")
    @ApiOperation("不分页查询")
    public AjaxResult listAll(@RequestBody WorkBill workBill) {
        startOrderBy();
        List<WorkBill> list = workBillService.selectWorkBillList(workBill);
        return success(list);
    }

    /**
     * 导出计件工作清单列表
     */
    @Log(title = "计件工作清单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, WorkBill workBill) {
        List<WorkBill> list = workBillService.selectWorkBillList(workBill);
        ExcelUtil<WorkBill> util = new ExcelUtil<WorkBill>(WorkBill.class);
        util.exportExcel(response, list, "计件工作清单数据");
    }

    /**
     * 获取计件工作清单详细信息
     */
    @GetMapping(value = "/info/{id}")
    @ApiOperation("详情")
    public AjaxResult info(@PathVariable("id") Long id) {
        return success(workBillService.selectWorkBillById(id));
    }

    /**
     * 新增计件工作清单
     */
    @Log(title = "计件工作清单", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增")
    public AjaxResult save(@RequestBody WorkBill workBill) {
        SysUser user = getLoginUser().getUser();
        boolean ok = workBillService.checkNo(workBill);

        if(!ok) return error("请确保工单号唯一");

        if (workBill.getId() == null) {
            workBill.setCreateId(user.getUserId());
            workBill.setCreateTime(DateUtils.getNowDate());
            workBill.setCreateBy(user.getNickName());
            workBillService.insertWorkBill(workBill);
        } else {
            workBill.setUpdateId(user.getUserId());
            workBill.setUpdateTime(DateUtils.getNowDate());
            workBill.setUpdateBy(user.getNickName());
            workBillService.updateWorkBill(workBill);
        }

        return success(workBill);
    }

    /**
     * 修改计件工作清单
     */
    @Log(title = "计件工作清单", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody WorkBill workBill) {
        SysUser user = getLoginUser().getUser();
        workBill.setUpdateId(user.getUserId());
        workBill.setUpdateTime(DateUtils.getNowDate());
        workBill.setUpdateBy(user.getNickName());
        return success(workBillService.updateWorkBill(workBill));
    }

    /**
     * 删除计件工作清单
     */
    @Log(title = "计件工作清单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除或批量删除")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(workBillService.deleteWorkBillByIds(ids));
    }

    @Log(title = "计件工作清单", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("条件删除")
    public ResponseModel delete(@RequestBody WorkBill workBill) {
        int num = workBillService.delete(workBill);
        return ResponseModel.of(num);
    }

    @PostMapping("/queryPage")
    @ApiOperation("分页查询-mp")
    public ResponseModel<Pages<WorkBill>> queryPage(@RequestBody WorkBill workBill) {
        Pages<WorkBill> page = workBillService.queryPage(workBill);
        return ResponseModel.of(page);
    }

    @PostMapping("/billReport")
    @ApiOperation("工单报表")
    public AjaxResult billReport(@RequestBody BillReportVo billReportVo) {
        List<BillReportVo> list = workBillService.queryBillReport(billReportVo);
        return AjaxResult.success(list);
    }

}
